package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.apkextract.lib.model.AeExportInfo;
import com.stark.apkextract.lib.model.AeExporter;
import com.stark.apkextract.lib.model.AeUtil;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.AppAdapter;
import flc.ast.bean.ApkBean;
import flc.ast.databinding.ActivityExportRecordBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;

public class ExportRecordActivity extends BaseAc<ActivityExportRecordBinding> {

    private AppAdapter appAdapter;
    private boolean isEdit = false;
    private List<ApkBean> listSel = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_export_record;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivExportRecordBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvApkList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        appAdapter = new AppAdapter();
        mDataBinding.rvApkList.setAdapter(appAdapter);
        appAdapter.setOnItemClickListener(this);

        mDataBinding.ivExportManage.setOnClickListener(this);
        mDataBinding.ivManageDelete.setOnClickListener(this);
        mDataBinding.ivManageCancel.setOnClickListener(this);
        mDataBinding.ivManageInstall.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivExportManage:
                isEdit = true;
                appAdapter.setEdit(isEdit);
                for (ApkBean validDatum : appAdapter.getValidData()) {
                    validDatum.setSelected(false);
                }
                appAdapter.notifyDataSetChanged();
                mDataBinding.ivExportManage.setVisibility(View.GONE);
                mDataBinding.llManage.setVisibility(View.VISIBLE);
                break;
            case R.id.ivManageCancel://取消
                isEdit = false;
                appAdapter.setEdit(isEdit);
                appAdapter.notifyDataSetChanged();
                listSel.clear();
                mDataBinding.ivExportManage.setVisibility(View.VISIBLE);
                mDataBinding.llManage.setVisibility(View.GONE);
                break;
            case R.id.ivManageDelete://删除
                if (listSel.size() == 0) {
                    return;
                }
                for (ApkBean apkBean : listSel) {
                    AeExporter.getInstance().delExport(apkBean.info);
                }
                isEdit = false;
                appAdapter.setEdit(isEdit);
                appAdapter.notifyDataSetChanged();
                listSel.clear();
                loadApks();
                ToastUtils.showShort(getString(R.string.delete_suc));
                mDataBinding.ivExportManage.setVisibility(View.VISIBLE);
                mDataBinding.llManage.setVisibility(View.GONE);
                break;
            case R.id.ivManageInstall://安装
                if (listSel.size() != 1) {
                    ToastUtils.showShort(getString(R.string.please_sel_one));
                    return;
                }
                //安装
                startActivity(IntentUtils.getInstallAppIntent(AppUtils.getAppPath(listSel.get(0).pkgName)));
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (appAdapter.getItem(position).isSelected()) {
                appAdapter.getItem(position).setSelected(false);
                listSel.remove(appAdapter.getItem(position));
                appAdapter.notifyDataSetChanged();
            } else {
                listSel.add(appAdapter.getItem(position));
                appAdapter.getItem(position).setSelected(true);
                appAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void initData() {
        loadApks();
    }

    private void loadApks() {
        showDialog(getString(com.stark.common.res.R.string.loading));
        RxUtil.create(new RxUtil.Callback<List<AeExportInfo>>() {
            @Override
            public void doBackground(ObservableEmitter<List<AeExportInfo>> emitter) {
                emitter.onNext(AeExporter.getInstance().getExportInfos());
            }

            @Override
            public void accept(List<AeExportInfo> packageNames) {
                if (!CollectionUtils.isEmpty(packageNames)) {
                    mDataBinding.rvApkList.setVisibility(View.VISIBLE);
                    mDataBinding.ivExportRecordNoData.setVisibility(View.GONE);
                    List<ApkBean> apkBeans = new ArrayList<>();
                    ApkBean bean;
                    for (AeExportInfo packageName : packageNames) {
                        bean = new ApkBean();
                        bean.pkgName = packageName.packageName;
                        bean.info = packageName;
                        apkBeans.add(bean);
                    }
                    appAdapter.setList(apkBeans);
                } else {
                    mDataBinding.rvApkList.setVisibility(View.GONE);
                    mDataBinding.ivExportRecordNoData.setVisibility(View.VISIBLE);
                }
                dismissDialog(500L);
            }
        });
    }

}